/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao.implementacoes;

import dao.Conexao;
import dao.EnderecoDao;
import dao.ImobiliariaDaoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.Endereco;

/**
 *
 * @author cristiane
 */
public class EnderecoDaoImpl implements EnderecoDao{


    private Connection conn;

    public EnderecoDaoImpl() throws ImobiliariaDaoException{
        try{
            conn = Conexao.getConnection();
        }catch(Exception e){
            throw new ImobiliariaDaoException("Erro: " + e.getMessage());
        }
    }

    public void salvarEndereco(Endereco endereco) throws ImobiliariaDaoException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if(endereco == null)
            throw new ImobiliariaDaoException("Nenhum endereco foi informado!");
        try{
            String sql = "INSERT INTO endereco VALUES (?, ?, ?, ?, ?, ?, ?)";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, endereco.getCodigoImovel());
            ps.setString(2, endereco.getRua());
            ps.setString(3, endereco.getNumero());
            ps.setString(4, endereco.getComplemento());
            ps.setString(5, endereco.getBairro());
            ps.setString(6, endereco.getCidade());
            ps.setString(7, endereco.getEstado());

            ps.executeUpdate();
        }catch(SQLException e){
            throw new ImobiliariaDaoException("Erro ao salvar endereco " + e);
        }finally{
            Conexao.closeConnection(conn1, ps);
        }
    }

    public void atualizarEndereco(Endereco endereco) throws ImobiliariaDaoException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if(endereco == null)
            throw new ImobiliariaDaoException("Nenhum endereco foi informado!");
        try{
            String sql = "UPDATE endereco SET rua=?, numero=?, complemento=?, bairro=?, " +
                    "cidade=?, estado=? WHERE codigoImovel=?";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, endereco.getRua());
            ps.setString(2, endereco.getNumero());
            ps.setString(3, endereco.getComplemento());
            ps.setString(4, endereco.getBairro());
            ps.setString(5, endereco.getCidade());
            ps.setString(6, endereco.getEstado());
            ps.setString(7, endereco.getCodigoImovel());

            ps.executeUpdate();
        }catch(SQLException e){
            throw new ImobiliariaDaoException("Erro ao salvar endereco " + e);
        }finally{
            Conexao.closeConnection(conn1, ps);
        }

    }

    public void excEndereco(Endereco endereco) throws ImobiliariaDaoException {
        PreparedStatement ps = null;
        Connection conn1 = null;
        if(endereco == null)
            throw new ImobiliariaDaoException("Nenhum endereco foi informado!");
        try{
            String sql = "DELETE FROM endereco WHERE codigoImovel=?";
            conn1 = conn;
            ps = conn1.prepareStatement(sql);
            ps.setString(1, endereco.getCodigoImovel());
            ps.executeUpdate();
        }catch(SQLException e){
            throw new ImobiliariaDaoException("Erro ao deletar endereco " + e);
        }finally{
            Conexao.closeConnection(conn1, ps);
        }
    }


}
